package sebjul.ecom.dao;

import java.util.*;
import javax.ejb.*;
import javax.persistence.*;
import sebjul.ecom.entities.Genre;


@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class GenreDao {

	@PersistenceContext
	EntityManager em;
	
	public Genre createGenre(Genre genre) {
		em.persist(genre);
		return genre;
	}
	
	public Genre findGenreById (int id) {
		return em.find(Genre.class, id);
	}
	
	public Genre findGenreByName (String name) {
		Query query = em.createNamedQuery("findGenreByName");
		query.setParameter("name", name);
		List<Genre> list = query.getResultList();
		if (list.size() > 0)
			return list.get(0);
		return null;
	}
	
	public Genre findGenreByIdJoin (int id) {
		Query query = em.createNamedQuery("findGenreByIdJoin");
		query.setParameter("id", id);
		List<Genre> list = query.getResultList();
		if (list.size() > 0)
			return list.get(0);
		return null;
	}
	
	public List<Genre> findGenres() {
		Query query = em.createNamedQuery("findAllGenres");
		return query.getResultList();
	}
	
	public void deleteGenre(Genre genre) {
		em.remove(em.merge(genre));
	}
	
	public Genre updateGenre(Genre genre) {
		return em.merge(genre);
	}
}
